# References / Acknowledgements for the Parser: http://www.unix.com/shell-programming-scripting/39734-how-parse-config-variables-external-file-shell-script.html

# Function: get_config_list config_file
# Purpose : Print the list of configs from config file
get_config_list()
{
	typeset config_file=$1

	awk -F '[][]' '
		NF==3 && $0 ~ /^\[.*\]/ { print $2 }
	' ${config_file}
}

# Function : set_config_vars config_file config [var_prefix]
# Purpose  : Set variables (optionaly prefixed by var_prefix) from config in config file
set_config_vars()
{
	typeset config_file=$1
	typeset config=$2
	typeset var_prefix=$3
	typeset config_vars

	config_vars=$( 
			awk -F= -v Config="${config}" -v Prefix="${var_prefix}" '
			BEGIN { 
				Config = toupper(Config);
				patternConfig = "\\[" Config "]";
			}
			toupper($0)  ~ patternConfig,(/\[/ && toupper($0) !~ patternConfig)  { 
				if (/\[/ || NF <2) next;
				sub(/^[[:space:]]*/, "");
				sub(/[[:space:]]*=[[:space:]]/, "=");
				print Prefix $0;
			} ' ${config_file} )

	eval "${config_vars}"
	echo Configuration parameters are : ${config_vars}
}

#
# Set variables for all config from config file
#
file=mymanet.conf

for cfg in $(get_config_list ${file})
do
	echo "--- Configuration [${cfg}] ---"
	unset $(set | awk -F= '/^cfg_/  { print $1 }') MYMANET_
	set_config_vars ${file} ${cfg} MYMANET_
	set | grep ^MYMANET_
done

export MYMANET_TOLERANCE
export MYMANET_IS_EMB_DEV
export MYMANET_ALPHA
export MYMANET_BETA

bash scripts/compile_load.sh
bash scripts/load_gateway.sh
bash scripts/load_client.sh
bash scripts/update_lan_ip.sh &
echo "IP_UPDATE "$$ > config/update_ip.conf

#Now, start the MyMANET traffic and Global Network Statistics Module (GNSM)
bash scripts/start_GNSM.sh

